/**
 * Created by 功振 on 2016/1/30.
 */
module.factory('buildAddition', function ($http) {
    return function (jobInfo) {
        return $http.post('/opBuildInfo/createJob', jobInfo).then(function (response) {
            return response;
        });
    };
});

angular.module('NatureCloud').factory('buildDelete', function ($http) {
    return function (jobInfo) {
        function dump_obj(myObject) {
            var s = "";
            for (var property in myObject) {
                s = s + "\n " + property + ": " + myObject[property];
            }
            return s;
        }

        //alert("DeleteInfo:" + dump_obj(jobInfo));
        return $http.post('/opBuildInfo/deleteJob', jobInfo).then(function (response) {
            return response.data;
        });
    };
});

angular.module('NatureCloud').factory('buildUpdate', function ($http) {
    return function (jobInfo) {
        return $http.post('/opBuildInfo/updateJob', jobInfo).then(function (response) {
            return response;
        });
    };
});

angular.module('NatureCloud').factory('buildQuery', function ($http) {
    return function (user, jobID) {
        return $http.get('/opBuildInfo/getJob?id=' + jobID + "&user=" + user).then(function (response) {
            return response.data;
        });
    };
});
angular.module('NatureCloud').factory('buildLastQuery', function ($http) {
    return function (user ,jobID) {
        return $http.get('/opBuildInfo/getLastBuild?id=' + jobID + "&user=" + user).then(function (response) {
            return response.data;
        });
    }
});
angular.module('NatureCloud').factory('buildListQuery', function ($http) {
    return function (user) {
        return $http.get('/opBuildInfo/listJobs?user=' + user).then(function (response) {
            //alert("user-:" + user + " response.data:"+ response.toString());
            return response.data;
        });
    };
});
angular.module('NatureCloud').factory('dockerShow', function ($http) {
    return function (user,param) {
        return $http.get('/opBuildInfo/showDocker?user='+ user+'&id='+param.id+'&dockeradd='+param.dockeradd).then(function (response) {
           return response.data;
        });
    }
});
angular.module('NatureCloud').factory('dockerTest', function ($http){
    return function (user,params,version,branchType) {
        var testInfo={
            user:user,
            id:params.id,
            tag: version,
            type:params.type,
            branchtype:branchType
        };
        return $http.post('/opBuildInfo/testDocker',testInfo).then(function (response) {
            return response.data;
        });
    }
});
angular.module('NatureCloud').factory('buildStart', function ($http) {
    return function (user, param, version,branchType,dockerfile) {
        var buildInfo = {
            user: user,
            id: param.id,
            tag: version,
            type:param.type,
            branchtype:branchType,
            _dockerfile:dockerfile,
            isOld:param.isOld
        };
        return $http.post('/opBuildInfo/buildJob', buildInfo).then(function (response) {
            return response.data;
        });
    };
});

angular.module('NatureCloud').factory('buildStop', function ($http) {
    return function (user, jobID, runID) {
        var url = '/opBuildInfo/stopJob?id=' + jobID + '&number=' + runID + "&user=" + user;
        //alert("url:" + url);
        return $http.get(url).then(function (response) {
            return response;
        });
    };
});

angular.module('NatureCloud').factory('builderService', function ($http) {
    return {
        createView: function (user) {
            var viewInfo = {
                id: user
            };
            return $http.post('/opBuildInfo/createUsrView', viewInfo).then(function (response) {
                return response;
            });
        },
        getBuildConOut: function (jobName, buildSeq, start) {
            return $http.get('/opBuildInfo/getBuildConOut?jobname=' + jobName + '&buildseq=' + buildSeq + '&start='+ start).then(function (response) {
                return response;
            });
        },
        getBuildConOutRunning:function (jobName, buildSeq, start) {
            return $http.get('/opBuildInfo/getBuildConOutRunning?jobname=' + jobName + '&buildseq=' + buildSeq + '&start='+ start).then(function(response){
                return response;
            });
        }
    };
});

angular.module('NatureCloud').factory('getWebhook',function ($http) {
    return function (user,jobID) {
        var jobInfo = {
            id : jobID,
            user : user
        };
        return $http.post('/opBuildInfo/getWebhook', jobInfo).then(function (response) {
            return response.data;
        });
    }
});
angular.module('NatureCloud').factory('isold',function ($http) {
    return function (jobID) {
        var jobInfo = {
            id : jobID
        };
        return $http.post('/opBuildInfo/isOld', jobInfo).then(function (response) {
            return response.data;
        });
    }
});
angular.module('NatureCloud').factory('updateWebhook',function ($http) {
    return function (user,jobID) {
        var jobInfo = {
            user : user,
            id : jobID
        };
        return $http.post('/opBuildInfo/updateWebhook',jobInfo).then(function (response) {
            return response.data;
        });
    }
});
angular.module('NatureCloud').factory('getBuildHistoryCount',function ($http) {
   return function (buildInfo) {
       return $http.post('/opBuildInfo/getBuildHistoryCount',buildInfo).then(function (response) {
          return response.data;
       });
   }
});
angular.module('NatureCloud').factory('updateBuildHistoryCount',function ($http) {
   return function (jobID) {
       return $http.post('/opBuildInfo/updateBuildHistoryCount',jobID).then(function (response) {
          return response.data;
       });
   }
});